package com.kongyz;

import com.kongyz.observer.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kongyz
 * @date 2018/7/3 21:40
 * @Description:
 */
public class Subject {
    private int state;
    private List<Observer> observers=new ArrayList<Observer>();

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObserver();
    }

    /**
     * 添加观察者
     * @param observer
     */
    public void attach(Observer observer){
        observers.add(observer);
    }

    /**
     * 通知所有的观察者
     */
    public void notifyAllObserver(){
        for (Observer model:observers){
            model.update();
        }
    }

}
